package libaryinterface;
import book.Login;
import jdbc.SearchJdbc;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserInterface {
    JFrame f1;
    JPanel p1;
    JButton bReturn, bBorrow, bBack;

    public UserInterface(String userid) {
        f1 = new JFrame("用户界面");
        p1 = new JPanel();
        bBorrow = new JButton("借阅书籍");
        bReturn = new JButton("归还书籍");
        bBack = new JButton("返回登录界面");

        p1.setLayout(null);
        bReturn.setBounds(150, 100, 100, 40);
        bBorrow.setBounds(150, 160, 100, 40);
        bBack.setBounds(140,220,120,40);

        f1.add(p1);
        p1.add(bBorrow);
        p1.add(bReturn);
        p1.add(bBack);

        f1.setVisible(true);
        f1.setBounds(500, 250, 400, 400);

        bBorrow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LendSearchInterface(userid);
            }
        });
        bReturn.addActionListener(new ActionListener() {
            ResultSet result;

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //先查找当前用户借的书
                    result = new SearchJdbc().searchUserid(userid);
                    if (result.next()) {
                        //进入归还书籍界面
                        new ReturnInterface(result, userid);
                    } else {
                        new MyDialogDemo("暂未借阅书籍!");
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        bBack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login();
               // f1.dispose();
            }
        });
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}